package com.aspsine.multithreaddownload;

import com.aspsine.multithreaddownload.demo.DataSource;
import com.aspsine.multithreaddownload.demo.callbacks.DownloadCallback;
import com.aspsine.multithreaddownload.demo.entity.AppInfo;
import com.aspsine.multithreaddownload.demo.util.Utils;
import ohos.app.Context;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.util.List;

public class DownloadOhosTest {

    Context app = MyApplication.getGlobalContext();

    @Test
    public void testDownloadSaveFile(){
        try {
            File file = Utils.mDownloadDir;
            Assert.assertNotNull(file);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Test
    public void testGetDataSource(){
        List<AppInfo> appInfoList =  DataSource.getInstance().getData();
        Assert.assertNotNull(appInfoList);
    }

    @Test
    public void testDownloadUrl(){
        AppInfo appInfo = new AppInfo();
        appInfo.setUrl("www.baidu.com");
        appInfo.setName("百度");
        try {
            DownloadRequest.Builder builder = new DownloadRequest.Builder()
                    .setName(appInfo.getName() + ".jpg")
                    .setUri(appInfo.getUrl())
                    .setFolder(Utils.mDownloadDir);
            DownloadRequest request = builder.build();
            Assert.assertEquals(request.getUri(), "www.baidu.com");
            Assert.assertEquals(request.getName(),"百度.jpg");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Test
    public void testDownloadCallback(){
        DownloadConfiguration configuration = new DownloadConfiguration();
        configuration.setMaxThreadNum(10);
        configuration.setThreadNum(3);
        DownloadManager.getInstance().init(app, configuration);

        AppInfo appInfo = new AppInfo();
        appInfo.setUrl("www.baidu.com");
        appInfo.setName("百度");
        try {
            DownloadRequest request = new DownloadRequest.Builder()
                    .setName(appInfo.getName() + ".jpg")
                    .setUri(appInfo.getUrl())
                    .setFolder(Utils.mDownloadDir)
                    .build();

            DownloadCallback callback = new DownloadCallback(0, appInfo);
            Assert.assertNotNull(callback);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Test
    public void testDownloadManagerInstance(){
        try {
            DownloadManager.getInstance().init(app);
            DownloadInfo downloadInfo = DownloadManager.getInstance().
                    getDownloadInfo("1");
            Assert.assertTrue(downloadInfo == null);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void testDownloadState(){
        final boolean[] onStarted = {false};
        try{
            DownloadRequest request = new DownloadRequest.Builder()
                    .setName("测试下载"+".jpg")
                    .setUri("https://wallroom.io/img/5120x3200/bg-2a45794.jpg")
                    .setFolder(Utils.mDownloadDir)
                    .build();
            DownloadManager.getInstance().download(request, request.getUri(), new CallBack() {
                @Override
                public void onStarted() {
                    onStarted[0] = true;
                }

                @Override
                public void onConnecting() {
                }

                @Override
                public void onConnected(long total, boolean isRangeSupport) {
                }

                @Override
                public void onProgress(long finished, long total, int progress) {
                }

                @Override
                public void onCompleted() {
                }

                @Override
                public void onDownloadPaused() {
                }

                @Override
                public void onDownloadCanceled() {
                }

                @Override
                public void onFailed(DownloadException e) {
                }
            });

            Assert.assertTrue(onStarted[0]);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
